package com.byq.mm.smartlib.obd.commands.control;

import com.byq.mm.smartlib.obd.commands.AbstractObdCommand;
import com.byq.mm.smartlib.obd.enums.AvailableCommandNames;


/**
 * * In order to get ECU Trouble Codes, one must first send a
 * DtcNumberObdCommand * and by so, determining the number of error codes
 * available by means of * getTotalAvailableCodes(). * * If none are available
 * (totalCodes < 1), don't instantiate this command.
 */
public class TroubleCodesObdCommand extends AbstractObdCommand {
	protected final static char[] dtcLetters = { 'P', 'C', 'B', 'U' };
	private StringBuffer codes = null;
	private int howManyTroubleCodes = 0;

	/** * Default ctor. */
	public TroubleCodesObdCommand(int howManyTroubleCodes) {
		super("03");
		codes = new StringBuffer();
		this.howManyTroubleCodes = howManyTroubleCodes;
	}

	/** * Copy ctor. * * @param other */
	public TroubleCodesObdCommand(TroubleCodesObdCommand other) {
		super(other);
		codes = new StringBuffer();
	}

	/** * @return the formatted result of this command in string representation. */
	public String formatResult() {
		String res = getResult();
		if (!"NODATA".equals(res)) { /*                           
									 * * Ignore first byte [43] of the response
									 * and then read each two * bytes.
									 */
			int begin = 2;
			int end = 6;
			for (int i = 0; i < howManyTroubleCodes * 2; i++) {
				byte b1 = Byte.parseByte(res.substring(begin, end));
				begin += 2;
				end += 2;
				byte b2 = Byte.parseByte(res.substring(begin, end));
				begin += 2;
				end += 2;
				int tempValue = b1 << 8 | b2;
			}
		}
		String[] ress = res.split("\r");
		for (String r : ress) {
			String k = r.replace("\r", "");
			codes.append(k);
			codes.append("\n");
		}
		return codes.toString();
	}

	@Override
	public String getFormattedResult() {
		return null;
	}

	@Override
	public String getName() {
		return AvailableCommandNames.TROUBLE_CODES.getValue();
	}

	@Override
	public String getRequestKey() {
		// TODO Auto-generated method stub
		return "03";
	}
}